package com.wwwc.index.web.servlet;

import java.io.*;
import java.awt.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.ServletRequestWrapper;
import java.net.*;

import com.wwwc.util.web.MyMessage;
//import com.wwwc.index.web.database.*;
import com.wwwc.index.web.ejb.database.*;
import com.wwwc.util.web.MyHtml;

public class PageColorBean {
    private EJBDatabaseQuery ejbdb;
    /*
    public String getPageColor(String page_id) {
	StringBuffer sbf = new StringBuffer();
	try {
	    if (ejbdb == null) {
		ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	    }
	    
	    String query = "SELECT * FROM PAGE_COLOR WHERE PAGE_ID="+page_id+";";
	    ArrayList a = ejbdb.selectSingleRecord(query);

	    if(a == null || a.size()==0) {
		query = "SELECT * FROM PAGE_COLOR WHERE PAGE_ID=5;";
		a = ejbdb.selectSingleRecord(query);
	    }

	    String pc_id   = null;
	    String bgcolor = null;
	    String text    = null;
	    String link    = null;
	    String vlink   = null;
	    String alink   = null;

	    page_id = (String)a.get(2);
	    bgcolor = (String)a.get(3);
	    text    = (String)a.get(4);
	    link    = (String)a.get(5);
	    vlink   = (String)a.get(6);
	    alink   = (String)a.get(7);
	    
	    sbf.append("<BODY bgcolor='"+bgcolor+"' text='"+text+"' link='"+link+"' vlink='"+vlink+"' ");
	    sbf.append(" alink='"+alink+"' marginheight='0' marginwidth='0' topmargin='0' leftmargin='0'>");
	}
	catch (Exception e) {
	    sbf.append("<BODY>");
	}
	return sbf.toString();
    }
    */

    public StringBuffer setPageColor(String call_path, String mid, String cpc_id, boolean manager, boolean admin) {
	StringBuffer sbf = new StringBuffer();
	try {
	    if (ejbdb == null) {
		ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	    }

	    String query = "SELECT * FROM PAGE_COLOR ORDER BY PAGE_ID;";
	    ArrayList alist = ejbdb.select(query);
	    Iterator it = alist.iterator();
	    ArrayList a = null;

	    String page_id = null;
	    String bgcolor = null;
	    String text    = null;
	    String link    = null;
	    String vlink   = null;
	    String alink   = null;

	    sbf.append("<BR><BR>");
	    sbf.append("<FORM name='PageColor' method='post' action='"+call_path+"'>");
	    sbf.append("<TABLE border='1'>");
	    sbf.append("<TR>");
	    sbf.append("<TD>&nbsp;</TD>");
	    sbf.append("<TD><B>Background Color</B></TD>");
	    sbf.append("<TD><B>Text Color</B></TD>");
	    sbf.append("<TD><B>Unvisited Link Color</B></TD>");
	    sbf.append("<TD><B>Visited Link Color</B></TD>");
	    sbf.append("<TD><B>Alive Link Color</B></TD>");
	    sbf.append("</TR>");

	    while (it.hasNext()) {
		a = (ArrayList)it.next();
		page_id = (String)a.get(2);
		bgcolor = (String)a.get(3);
		text    = (String)a.get(4);
		link    = (String)a.get(5);
		vlink   = (String)a.get(6);
		alink   = (String)a.get(7);

		sbf.append("<TR>");
		if(cpc_id != null && cpc_id.equals(page_id)) {
		    sbf.append("<TD><input type='radio' name='pc_id' value='"+page_id+"' checked></TD>");
		}
		else {
		    sbf.append("<TD><input type='radio' name='pc_id' value='"+page_id+"'></TD>");
		}
		sbf.append("<TD bgcolor="+bgcolor+">&nbsp;</TD>");
		sbf.append("<TD bgcolor="+text+">&nbsp;</TD>");
		sbf.append("<TD bgcolor="+link+">&nbsp</TD>");
		sbf.append("<TD bgcolor="+vlink+">&nbsp</TD>");
		sbf.append("<TD bgcolor="+alink+">&nbsp;</TD>");
		sbf.append("</TR>");		
	    }

	    sbf.append("</TABLE>");
	    sbf.append("<input type='hidden' name='mid' value='"+mid+"'>");
	    sbf.append("</FORM>");

	    sbf.append("<BR><BR>");
	    sbf.append("<TABLE border='0'>");
	    sbf.append("<TR>");
	    sbf.append(MyHtml.makeLinkCell("Change","javaScript:document.PageColor.submit();"));
	    if(admin) {
		sbf.append(MyHtml.makeLinkCell("Edit", call_path+"?mid="+mid+"&aid=4-1"));
	    }
	    sbf.append("</TR>");
	    sbf.append("</TABLE>");
	}
	catch (Exception e) {
	}

	return sbf;
    }

    public StringBuffer editPageColor(HttpServletRequest request, String call_path, String mid, 
				      String save_call_back, String saved_call_back) {
	StringBuffer sbf = new StringBuffer();
	try {
	    if (ejbdb == null) {
		ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	    }

	    String rgb[][] = {{"00","00","00"},{"00","00","00"},{"00","00","00"},{"00","00","00"},{"00","00","00"}};
	    
	    String r = null;
	    String g = null;
	    String b = null;

	    for(int k=0; k<rgb.length;k++) {
		r = request.getParameter("option0"+k);
		g = request.getParameter("option1"+k);
		b = request.getParameter("option2"+k);
		
		if(r != null) {
		    rgb[k][0] = r;
		}
		if(g != null) {
		    rgb[k][1] = g;
		}
		if(b != null) {
		    rgb[k][2] = b;
		}
	    }
	    
	    String query = null;
	    String submit = request.getParameter("B");
	    String next_page_id = null;
	    String fid  = null;

	    if(submit != null && submit.equals("Add")) {
		next_page_id = request.getParameter("next_page_id");
		StringBuffer qbf = new StringBuffer();
		String srgb[] = null;
		for(int j = 0; j < rgb.length; j++) {
		    srgb = rgb[j];
		    qbf.append("'#");
		    for(int n = 0; n < srgb.length; n++) {
			qbf.append(srgb[n]);
		    }
		    if(j == rgb.length-1) {
			qbf.append("'");
		    }
		    else {
			qbf.append("',");
		    }
		}
		query = "INSERT INTO PAGE_COLOR VALUES (null, 'guest',"+next_page_id+","+qbf.toString()+");";
		ejbdb.insert(query);
	    }

	    if(submit != null && submit.equals("Save")) {
		fid = request.getParameter("fid");
		if(fid != null) {
		    query = "UPDATE PAGE_COLOR SET "+
			"BGCOLOR = '"+request.getParameter("bgcolor")+"',"+
			"TEXT    = '"+request.getParameter   ("text")+"',"+
			"LINK    = '"+request.getParameter   ("link")+"',"+
			"VLINK   = '"+request.getParameter  ("vlink")+"',"+
			"ALINK   = '"+request.getParameter  ("alink")+"' WHERE ID="+fid+";";
		    ejbdb.update(query);
		}
	    }

	    if(submit != null && submit.equals("2")) {
		fid = request.getParameter("fid");
		if(fid != null) {
		    query = "DELETE FROM PAGE_COLOR WHERE ID="+fid;
		    ejbdb.delete(query);
		}
	    }

	    query = "SELECT * FROM PAGE_COLOR ORDER BY PAGE_ID;";

	    ArrayList alist = ejbdb.select(query);
	    Iterator it = alist.iterator();
	    ArrayList a = null;
	    String id = null;
	    String page_id = null;
	    String bgcolor = null;
	    String text    = null;
	    String link    = null;
	    String vlink   = null;
	    String alink   = null;
		
	    sbf.append("<BR><BR>");
	    sbf.append("<FORM name='PageColor' method='post' action='"+call_path+"'>");
	    sbf.append("<TABLE border='1'>");
	    sbf.append("<TR>");
	    sbf.append("<TD colspan='2' align='center'>Background</TD>");
	    sbf.append("<TD colspan='2' align='center'>Text</TD>");
	    sbf.append("<TD colspan='2' align='center'>Unvisited Link</TD>");
	    sbf.append("<TD colspan='2' align='center'>Visited Link</TD>");
	    sbf.append("<TD colspan='2' align='center'>Alive Link</TD>");
	    sbf.append("<TD colspan='2' align='center'>Delete/Edit</TD>");
	    sbf.append("</TR>");
	    
	    next_page_id = ""+(alist.size()+1);

	    while (it.hasNext()) {
		a = (ArrayList)it.next();
		id      = (String)a.get(0);
		page_id = (String)a.get(2);
		bgcolor = (String)a.get(3);
		text    = (String)a.get(4);
		link    = (String)a.get(5);
		vlink   = (String)a.get(6);
		alink   = (String)a.get(7);

		fid = request.getParameter("fid");
		
		if(fid != null && fid.equals(id) && submit != null && submit.equals("1")) {
		    sbf.append("<TR>");
		    sbf.append("<TD bgcolor="+bgcolor+" width='50'>&nbsp;</TD>");
		    sbf.append("<TD><input type='text' name='bgcolor' value='"+bgcolor+"' style='font-size:8pt' size='8'></TD>");
		    sbf.append("<TD bgcolor="+text+" width='50'>&nbsp;</TD>");
		    sbf.append("<TD><input type='text' name='text' value='"+text+"' style='font-size:8pt' size='8'></TD>");
		    sbf.append("<TD bgcolor="+link+" width='50'>&nbsp;</TD>");
		    sbf.append("<TD><input type='text' name='link' value='"+link+"' style='font-size:8pt' size='8'></TD>");
		    sbf.append("<TD bgcolor="+vlink+" width='50'>&nbsp;</TD>");
		    sbf.append("<TD><input type='text' name='vlink' value='"+vlink+"' style='font-size:8pt' size='8'></TD>");
		    sbf.append("<TD bgcolor="+alink+" width='50'>&nbsp;</TD>");
		    sbf.append("<TD><input type='text' name='alink' value='"+alink+"' style='font-size:8pt' size='8'></TD>");
		    sbf.append(MyHtml.makeLinkCell("Delete", call_path+"?mid="+mid+"&aid="+save_call_back+"&fid="+id+"&B=2"));
		    sbf.append("<TD align='center'><input type='submit' name='B' value='Save' style='font-size:8pt'></TD>");
		    sbf.append("</TR>");	
		    sbf.append("<input type='hidden' name='fid' value='"+id+"'>");
		}
		else {
		    sbf.append("<TR>");
		    sbf.append("<TD bgcolor="+bgcolor+" width='50'>&nbsp;</TD>");
		    sbf.append("<TD>"+bgcolor+"</TD>");
		    sbf.append("<TD bgcolor="+text+" width='50'>&nbsp;</TD>");
		    sbf.append("<TD>"+text+"</TD>");
		    sbf.append("<TD bgcolor="+link+" width='50'>&nbsp;</TD>");
		    sbf.append("<TD>"+link+"</TD>");
		    sbf.append("<TD bgcolor="+vlink+" width='50'>&nbsp;</TD>");
		    sbf.append("<TD>"+vlink+"</TD>");
		    sbf.append("<TD bgcolor="+alink+" width='50'>&nbsp;</TD>");
		    sbf.append("<TD>"+alink+"</TD>");
		    sbf.append(MyHtml.makeLinkCell("Delete", call_path+"?mid="+mid+"&aid="+save_call_back+"&fid="+id+"&B=2"));
		    sbf.append(MyHtml.makeLinkCell("Edit", call_path+"?mid="+mid+"&aid="+save_call_back+"&fid="+id+"&B=1"));
		    sbf.append("</TR>");	
		}	
	    }
	
	    sbf.append("<BR><BR>");

	    sbf.append("<TR>");
	    sbf.append("<TD colspan='2' align='center'>"+ rgbOptions(rgb[0],0)+"</TD>");
	    sbf.append("<TD colspan='2' align='center'>"+ rgbOptions(rgb[1],1)+"</TD>");
	    sbf.append("<TD colspan='2' align='center'>"+ rgbOptions(rgb[2],2)+"</TD>");
	    sbf.append("<TD colspan='2' align='center'>"+ rgbOptions(rgb[3],3)+"</TD>");
	    sbf.append("<TD colspan='2' align='center'>"+ rgbOptions(rgb[4],4)+"</TD>");
	    sbf.append("<TD align='center'><input type='submit' name='B' value='Add' style='font-size:8pt'></TD>");
	    sbf.append("<TD align='center'><input type='reset' name='B' value='Reset' style='font-size:8pt'></TD>");
	    sbf.append("</TR>");	

	    sbf.append("</TABLE>");

	    sbf.append("<input type='hidden' name='mid' value='"+mid+"'>");
	    sbf.append("<input type='hidden' name='aid' value='"+save_call_back+"'>");
	    sbf.append("<input type='hidden' name='next_page_id' value='"+next_page_id+"'>");
	    sbf.append("</FORM>");
	}
	catch (Exception e) {
	}

	return sbf;
    }

    private StringBuffer rgbOptions (String[] rgb, int which) {
	String colors[] = {"00","10","20","30","40","50","60","70","80","90","AA","BB","CC","DD","EE","FF"};
	StringBuffer sbf = new StringBuffer();
	sbf.append("\n<TABLE cellSpacing='0' cellPadding='0' border='1'>");
	sbf.append("<TR>");
	sbf.append("<TD bgcolor='#"+rgb[0]+rgb[1]+rgb[2]+"' colspan='3'>&nbsp;</TD>");
	sbf.append("</TR>");
	sbf.append("<TR><TD align='center'><B>R</B></TD><TD align='center'><B>G</B></TD><TD align='center'><B>B</B></TD></TR>");
	sbf.append("<TR>");
	
	String selected = null;
	String temp = null;
	for (int m =0; m<rgb.length; m++) {
	    selected = rgb[m];
	    sbf.append("<TD><SELECT name='option"+m+""+which+"' onChange='this.form.submit();' ");
	    sbf.append(" style='font-size: 10px'>");
	    for(int i=0; i < colors.length; i++) {
		temp = colors[i];
		if(temp.equals(selected)) {
		    sbf.append("<option selected>"+temp+"</option>");
		}
		else {
		    sbf.append("<option>"+temp+"</option>");
		}
	    }
	    sbf.append("</SELECT></TD>");
	}
	sbf.append("</TR>");
	sbf.append("</TABLE>");
	return sbf;
    }

    public static StringBuffer getScreenInfo (HttpServletRequest request) {
	String call_path = request.getContextPath()+request.getServletPath();
	StringBuffer sbf = new StringBuffer();
	sbf.append("<html>");
	sbf.append("<body>");
	sbf.append("<form name='screen_form' method='post' action='"+call_path+"'>");
	String name = null;
	String value = null;
	Enumeration enum = request.getParameterNames();
	while(enum.hasMoreElements()) {
	    name  = (String)enum.nextElement();
	    value = request.getParameter(name);
	    if(value != null) {
		sbf.append("<input type='hidden' name='"+name+"' value='"+value+"'>");
	    }
	}
	sbf.append("<input type='hidden' name='screen_check' value='true'>");
	sbf.append("<input type='hidden' name='sw' value=''>");
	sbf.append("<input type='hidden' name='sh' value=''>");
	sbf.append("</form>\n"); 
	sbf.append("<SCRIPT>");
	sbf.append("document.screen_form.sw.value=screen.width;");
	sbf.append("document.screen_form.sh.value=screen.height;");
	sbf.append("document.screen_form.submit();");
	sbf.append("</SCRIPT>");
	sbf.append("</body>\n"); 
	sbf.append("</html>\n"); 
	return sbf;
    }
}






